<script>
import InterviewListItem from "@/components/InterviewListItem.vue";

import {getInterviewList} from "@/api/Interview";

export default {
  name: 'InterviewList',
  components: {InterviewListItem},
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      current: 0,
      pageSize: 5,
    };
  },
  props:{
    sorter:{
      type: String,
      default: ''
    },
    optType:{
      type: Number,
      default: 0
    }

  },
  methods: {
    async onLoad() {
      this.current++;
      if (this.refreshing) {
        this.current = 1;
        this.list = [];
      }

      await this.getList();

      this.refreshing = false;
      this.loading = false;

      if (this.list.length < this.current*this.pageSize) {
        this.finished = true;
      }
    },
    onRefresh() {
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    async getList() {
      let params={
        current: this.current,
        pageSize: this.pageSize,
      }
      let url = "/h5/interview/query"
      if (this.optType) {
        params={
          optType: this.optType,
          pageSize: this.pageSize,
          page: this.current
        }
        url = "/h5/interview/opt/list"
      }
      if (this.sorter) {
        params['sorter']=this.sorter
      }
      const res = await getInterviewList(url,params)
      // console.log(res);
      this.list.push(...(res.data.rows));
    },
    onClickItem(item) {
      this.$router.push({
        path: '/detail',
        query: {
          id: item.id
        }
      })
    }
  },
}
</script>

<template>
  <div class="list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
      >
        <InterviewListItem v-for="item in list" :key="item.id" :obj="item" @onClickItem="onClickItem(item)">
          <van-divider />
        </InterviewListItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<style scoped lang="less">

</style>